package com.freewayso.image.combiner.painter;

import com.freewayso.image.combiner.element.CombineElement;
import com.freewayso.image.combiner.element.FrameElement;

import java.awt.*;
import java.io.IOException;

/**
 * @author lobyliang
 */
public class FramePainter implements IPainter{
    @Override
    public void draw(Graphics2D g, CombineElement element, int canvasWidth) throws IOException, Exception{
        FrameElement frameElement = (FrameElement) element;
        Color oldColor = g.getColor();
        Stroke oldStroke = g.getStroke();
        g.setColor(frameElement.getColor());
        BasicStroke stroke = new BasicStroke(frameElement.getStrokeWidth(), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 3.5f);//, new float[]{10, 5}, 0f);
        g.setStroke(stroke);
        int lineGap = (int) (frameElement.getStrokeWidth()/2);
        if(lineGap==0){
            lineGap = 1;
        }
        if(frameElement.getRoundCorner()!=null){
            int corner = frameElement.getRoundCorner();
            g.drawRoundRect(frameElement.getX()+lineGap, frameElement.getY()+lineGap,  frameElement.getWidth() -lineGap,frameElement.getHeight() - lineGap, corner,corner);
        }
        else
        {
            g.drawRect(frameElement.getX()+lineGap, frameElement.getY()+lineGap, frameElement.getWidth() - lineGap,frameElement.getHeight() -lineGap);
        }
        g.setColor(oldColor);
        g.setStroke(oldStroke);
    }
}
